<!DOCTYPE html>
<html lang="zh-CN" data-theme="light">

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width,initial-scale=1" />
  <meta name="generator" content="VuePress 2.0.0-beta.51" />
  <meta name="theme" content="VuePress Theme Hope" />
  <meta property="og:url" content="https://pro.autojs.org/docs/zh/v8/sensors.html">
  <meta property="og:site_name" content="Auto.js Pro文档">
  <meta property="og:title" content="sensors - 传感器">
  <meta property="og:type" content="article">
  <meta property="og:updated_time" content="2022-10-22T07:36:15.000Z">
  <meta property="og:locale" content="zh-CN">
  <meta property="og:locale:alternate" content="en-US">
  <meta property="article:modified_time" content="2022-10-22T07:36:15.000Z">
  <link rel="alternate" hreflang="en-us" href="https://pro.autojs.org/docs/en/zh/v8/sensors.html">
  <link rel="icon" href="https://pro.autojs.org/docs/favicon.ico">
  <link rel="manifest" href="https://pro.autojs.org/docs/manifest.webmanifest" crossorigin="use-credentials">
  <meta name="theme-color" content="#46bd87">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover">
  <title>sensors - 传感器 | Auto.js Pro文档</title>
  <meta name="description" content="支持Node.js的自动化工作流JavaScript IDE">
  <style>
    :root {
      --bg-color: #fff;
    }

    html[data-theme="dark"] {
      --bg-color: #1d2025;
    }

    html,
    body {
      background-color: var(--bg-color);
    }
  </style>
  <script>
    const userMode = localStorage.getItem("vuepress-theme-hope-scheme");
    const systemDarkMode =
      window.matchMedia &&
      window.matchMedia("(prefers-color-scheme: dark)").matches;

    if (userMode === "dark" || (userMode !== "light" && systemDarkMode)) {
      document.querySelector("html").setAttribute("data-theme", "dark");
    }
  </script>
  <link rel="stylesheet" href="../../assets/style.ae9cb182.css">
  <link rel="modulepreload" href="../../assets/app.6aa2b576.js">
  <link rel="modulepreload" href="../../assets/sensors.html.efa2e136.js">
  <link rel="modulepreload" href="../../assets/_plugin-vue_export-helper.cdc0426e.js">
  <link rel="modulepreload" href="../../assets/sensors.html.ac0887f8.js">
</head>

<body>


  <!-- Copyright � 2008. Spidersoft Ltd -->
  <style>
    A.applink:hover {
      border: 2px dotted #DCE6F4;
      padding: 2px;
      background-color: #ffff00;
      color: green;
      text-decoration: none
    }

    A.applink {
      border: 2px dotted #DCE6F4;
      padding: 2px;
      color: #2F5BFF;
      background: transparent;
      text-decoration: none
    }

    A.info {
      color: #2F5BFF;
      background: transparent;
      text-decoration: none
    }

    A.info:hover {
      color: green;
      background: transparent;
      text-decoration: underline
    }
  </style>


  <div id="app"><!--[--><!--[--><!--[--><span tabindex="-1"></span><a href="#main-content"
      class="skip-link sr-only">Skip to content</a><!--]-->
    <div class="theme-container has-toc"><!--[--><!--[-->
      <header class="navbar">
        <div class="navbar-left"><button class="toggle-sidebar-button" title="Toggle Sidebar"><span
              class="icon"></span></button><!----><a href="../../docs.html" class="brand"><img class="logo"
              src="../../logo.png" alt="Auto.js Pro文档"><!----><span class="site-name hide-in-pad">Auto.js
              Pro文档</span></a><!----></div>
        <div class="navbar-center"><!---->
          <nav class="nav-links">
            <div class="nav-item hide-in-mobile"><a href="../../docs.html" class="nav-link"
                aria-label="Auto.js Pro"><span class="icon iconfont icon-home"></span>Auto.js Pro<!----></a></div>
            <div class="nav-item hide-in-mobile"><a href="../guide/guide.html" class="nav-link" aria-label="指南"><span
                  class="icon iconfont icon-creative"></span>指南<!----></a></div>
            <div class="nav-item hide-in-mobile"><a href="v8.html" class="nav-link active" aria-label="第一代API文档"><span
                  class="icon iconfont icon-note"></span>第一代API文档<!----></a></div>
            <div class="nav-item hide-in-mobile"><a href="../v9/v9.html" class="nav-link" aria-label="第二代API文档"><span
                  class="icon iconfont icon-note"></span>第二代API文档<!----></a></div>
            <div class="nav-item hide-in-mobile">
              <div class="dropdown-wrapper"><button class="dropdown-title" type="button" aria-label="官网与相关链接"><span
                    class="title"><span class="icon iconfont icon-discover"></span>官网与相关链接</span><span
                    class="arrow"></span>
                  <ul class="nav-dropdown">
                    <li class="dropdown-item"><a href="../../../pro.autojs.html" rel="noopener noreferrer"
                        target="_blank" aria-label="官网与软件下载" class="nav-link"><!---->官网与软件下载<span><svg
                            class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
                            focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15">
                            <path fill="currentColor"
                              d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                            </path>
                            <polygon fill="currentColor"
                              points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                            </polygon>
                          </svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a>
                    </li>
                    <li class="dropdown-item"><a href="https://blog.autojs.org/" rel="noopener noreferrer"
                        target="_blank" aria-label="官方博客" class="nav-link"><!---->官方博客<span><svg
                            class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
                            focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15">
                            <path fill="currentColor"
                              d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                            </path>
                            <polygon fill="currentColor"
                              points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                            </polygon>
                          </svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a>
                    </li>
                    <li class="dropdown-item"><a href="https://pd.qq.com/s/gosos5" rel="noopener noreferrer"
                        target="_blank" aria-label="官方频道" class="nav-link"><!---->官方频道<span><svg
                            class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true"
                            focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15">
                            <path fill="currentColor"
                              d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z">
                            </path>
                            <polygon fill="currentColor"
                              points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9">
                            </polygon>
                          </svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a>
                    </li>
                  </ul>
                </button></div>
            </div>
          </nav><!---->
        </div>
        <div class="navbar-right"><!---->
          <div class="nav-item">
            <div class="dropdown-wrapper i18n-dropdown"><button class="dropdown-title" type="button"
                aria-label="选择语言"><!--[--><svg xmlns="http://www.w3.org/2000/svg" class="icon i18n-icon"
                  viewBox="0 0 1024 1024" fill="currentColor" aria-label="i18n icon"
                  style="width:1rem;height:1rem;vertical-align:middle;">
                  <path
                    d="M379.392 460.8 494.08 575.488l-42.496 102.4L307.2 532.48 138.24 701.44l-71.68-72.704L234.496 460.8l-45.056-45.056c-27.136-27.136-51.2-66.56-66.56-108.544h112.64c7.68 14.336 16.896 27.136 26.112 35.84l45.568 46.08 45.056-45.056C382.976 312.32 409.6 247.808 409.6 204.8H0V102.4h256V0h102.4v102.4h256v102.4H512c0 70.144-37.888 161.28-87.04 210.944L378.88 460.8zM576 870.4 512 1024H409.6l256-614.4H768l256 614.4H921.6l-64-153.6H576zM618.496 768h196.608L716.8 532.48 618.496 768z">
                  </path>
                </svg><!--]--><span class="arrow"></span>
                <ul class="nav-dropdown">
                  <li class="dropdown-item"><a href="../../en/en.html" class="nav-link"
                      aria-label="English"><!---->English<!----></a></li>
                  <li class="dropdown-item"><a aria-current="page" href="sensors.html"
                      class="router-link-active router-link-exact-active nav-link active"
                      aria-label="简体中文"><!---->简体中文<!----></a></li>
                </ul>
              </button></div>
          </div><!---->
          <div class="nav-item hide-in-mobile"><button id="appearance-switch"><svg xmlns="http://www.w3.org/2000/svg"
                class="icon auto-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="auto icon"
                style="display:block;">
                <path
                  d="M512 992C246.92 992 32 777.08 32 512S246.92 32 512 32s480 214.92 480 480-214.92 480-480 480zm0-840c-198.78 0-360 161.22-360 360 0 198.84 161.22 360 360 360s360-161.16 360-360c0-198.78-161.22-360-360-360zm0 660V212c165.72 0 300 134.34 300 300 0 165.72-134.28 300-300 300z">
                </path>
              </svg><svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" viewBox="0 0 1024 1024"
                fill="currentColor" aria-label="dark icon" style="display:none;">
                <path
                  d="M524.8 938.667h-4.267a439.893 439.893 0 0 1-313.173-134.4 446.293 446.293 0 0 1-11.093-597.334A432.213 432.213 0 0 1 366.933 90.027a42.667 42.667 0 0 1 45.227 9.386 42.667 42.667 0 0 1 10.24 42.667 358.4 358.4 0 0 0 82.773 375.893 361.387 361.387 0 0 0 376.747 82.774 42.667 42.667 0 0 1 54.187 55.04 433.493 433.493 0 0 1-99.84 154.88 438.613 438.613 0 0 1-311.467 128z">
                </path>
              </svg><svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" viewBox="0 0 1024 1024"
                fill="currentColor" aria-label="light icon" style="display:none;">
                <path
                  d="M952 552h-80a40 40 0 0 1 0-80h80a40 40 0 0 1 0 80zM801.88 280.08a41 41 0 0 1-57.96-57.96l57.96-58a41.04 41.04 0 0 1 58 58l-58 57.96zM512 752a240 240 0 1 1 0-480 240 240 0 0 1 0 480zm0-560a40 40 0 0 1-40-40V72a40 40 0 0 1 80 0v80a40 40 0 0 1-40 40zm-289.88 88.08-58-57.96a41.04 41.04 0 0 1 58-58l57.96 58a41 41 0 0 1-57.96 57.96zM192 512a40 40 0 0 1-40 40H72a40 40 0 0 1 0-80h80a40 40 0 0 1 40 40zm30.12 231.92a41 41 0 0 1 57.96 57.96l-57.96 58a41.04 41.04 0 0 1-58-58l58-57.96zM512 832a40 40 0 0 1 40 40v80a40 40 0 0 1-80 0v-80a40 40 0 0 1 40-40zm289.88-88.08 58 57.96a41.04 41.04 0 0 1-58 58l-57.96-58a41 41 0 0 1 57.96-57.96z">
                </path>
              </svg></button></div>
          <div class="search-box" role="search" data-v-f3875574><input value="" aria-label="Search" class=""
              placeholder="Search" autocomplete="off" spellcheck="false" data-v-f3875574><!----></div><!----><button
            class="toggle-navbar-button" aria-label="Toggle Navbar" aria-expanded="false"
            aria-controls="nav-screen"><span class="button-container"><span class="button-top"></span><span
                class="button-middle"></span><span class="button-bottom"></span></span></button>
        </div>
      </header><!----><!--]--><!---->
      <div class="toggle-sidebar-wrapper"><span class="arrow left"></span></div>
      <aside class="sidebar"><!--[--><!----><!--]-->
        <ul class="sidebar-links">
          <li>
            <section class="sidebar-group">
              <p class="sidebar-heading"><!----><span class="title">API文档</span><!----></p>
              <ul class="sidebar-links">
                <li>
                  <section class="sidebar-group"><button class="sidebar-heading clickable"><!----><span
                        class="title">自动化 -
                        控件操作</span><span class="arrow down"></span></button>
                    <ul class="sidebar-links">
                      <li><!--[--><a href="./automator/"
                          class="router-link-active router-link-exact-active nav-link sidebar-link sidebar-page"
                          aria-label="入门介绍" aria-current="page"><!---->入门介绍<!----></a>
                        <ul class="sidebar-sub-headers"></ul><!--]-->
                      </li>
                      <li><!--[--><a href="./automator/selector.html" class="nav-link sidebar-link sidebar-page"
                          aria-label="选择器"><!---->选择器<!----></a>
                        <ul class="sidebar-sub-headers"></ul><!--]-->
                      </li>
                      <li><!--[--><a href="./automator/uiobject.html" class="nav-link sidebar-link sidebar-page"
                          aria-label="控件和控件集合"><!---->控件和控件集合<!----></a>
                        <ul class="sidebar-sub-headers"></ul><!--]-->
                      </li>
                      <li><!--[--><a href="./automator/api.html" class="nav-link sidebar-link sidebar-page"
                          aria-label="automator与其他函数"><!---->automator与其他函数<!----></a>
                        <ul class="sidebar-sub-headers"></ul><!--]-->
                      </li>
                    </ul>
                  </section>
                </li>
                <li><!--[--><a href="./coordinatesBasedAutomation.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="自动化 - 坐标操作"><!---->自动化 -
                    坐标操作<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./app.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="app - 应用"><!---->app -
                    应用<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./base64.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="base64"><!---->base64<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./colors.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="colors - 颜色"><!---->colors - 颜色<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./canvas.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="canvas - 画布"><!---->canvas - 画布<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./console.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="console - 控制台"><!---->console - 控制台<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./crypto.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="crypto - 加解密与消息摘要"><!---->crypto - 加解密与消息摘要<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./debug.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="debug - 调试工具"><!---->debug - 调试工具<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./device.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="device - 设备"><!---->device - 设备<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./dialogs.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="dialogs - 对话框"><!---->dialogs - 对话框<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./engines.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="engines - 脚本引擎"><!---->engines - 脚本引擎<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./events.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="events - 事件与监听"><!---->events - 事件与监听<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./floaty.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="floaty - 悬浮窗"><!---->floaty - 悬浮窗<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./files.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="files - 文件系统"><!---->files - 文件系统<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./globals.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="globals - 全局变量与函数"><!---->globals - 全局变量与函数<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./http.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="http - HTTP网络请求"><!---->http - HTTP网络请求<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./images.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="images - 图片处理"><!---->images - 图片处理<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./keys.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="keys - 按键模拟"><!---->keys
                    - 按键模拟<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./media.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="media - 多媒体"><!---->media
                    - 多媒体<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./modules.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="module - 模块"><!---->module - 模块<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./ocr.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="ocr - 文字识别"><!---->ocr -
                    文字识别<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./plugins.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="plugins - 插件"><!---->plugins - 插件<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./powerManager.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="power_manager - 电源管理"><!---->power_manager - 电源管理<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./sensors.html" class="nav-link sidebar-link sidebar-page active"
                    aria-label="sensors - 传感器"><!---->sensors - 传感器<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./shell.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="shell - Shell命令"><!---->shell - Shell命令<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./storages.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="storages - 本地存储"><!---->storages - 本地存储<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./settings.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="settings - 设置"><!---->settings - 设置<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./threads.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="threads - 多线程"><!---->threads - 多线程<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./timers.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="timers - 定时器"><!---->timers - 定时器<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./timedTasks.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="work_manager - 定时与任务"><!---->work_manager - 定时与任务<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li>
                  <section class="sidebar-group"><button class="sidebar-heading clickable"><!----><span
                        class="title">ui - 用户界面</span><span class="arrow down"></span></button>
                    <ul class="sidebar-links">
                      <li><a href="/docs/zh/v8/ui/ui.html" class="nav-link sidebar-link sidebar-page"
                          aria-label="ui入门介绍"><!---->ui入门介绍<!----></a>
                        <ul class="sidebar-sub-headers"></ul>
                      </li>
                      <li><a href="/docs/zh/v8/ui/basic.html" class="nav-link sidebar-link sidebar-page"
                          aria-label="基础布局与控件"><!---->基础布局与控件<!----></a>
                        <ul class="sidebar-sub-headers"></ul>
                      </li>
                      <li><a href="/docs/zh/v8/ui/advanced.html" class="nav-link sidebar-link sidebar-page"
                          aria-label="其他布局与控件"><!---->其他布局与控件<!----></a>
                        <ul class="sidebar-sub-headers"></ul>
                      </li>
                      <li><a href="/docs/zh/v8/ui/api.html" class="nav-link sidebar-link sidebar-page"
                          aria-label="api文档"><!---->api文档<!----></a>
                        <ul class="sidebar-sub-headers"></ul>
                      </li>
                      <li><a href="/docs/zh/v8/ui/customWidget.html" class="nav-link sidebar-link sidebar-page"
                          aria-label="自定义控件"><!---->自定义控件<!----></a>
                        <ul class="sidebar-sub-headers"></ul>
                      </li>
                      <li><a aria-current="page" href="/docs/zh/v8/ui/webview.html"
                          class="router-link-active router-link-exact-active nav-link sidebar-link sidebar-page"
                          aria-label="使用Web编写界面"><!---->使用Web编写界面<!----></a>
                        <ul class="sidebar-sub-headers"></ul>
                      </li>
                    </ul>
                  </section>
                </li>
                <li><!--[--><a href="./util.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="util - 工具"><!---->util -
                    工具<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./webSocket.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="WebSocket"><!---->WebSocket<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="./zip.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="zip - 压缩与解压"><!---->zip -
                    压缩与解压<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
              </ul>
            </section>
          </li>
          <li>
            <section class="sidebar-group">
              <p class="sidebar-heading active"><!----><span class="title">API文档</span><!----></p>
              <ul class="sidebar-links">
                <li><!--[--><a href="app.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="app - 应用"><!---->app - 应用<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="base64.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="base64"><!---->base64<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="colors.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="colors - 颜色"><!---->colors - 颜色<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="canvas.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="canvas - 画布"><!---->canvas - 画布<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="console.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="console - 控制台"><!---->console - 控制台<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="crypto.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="crypto - 加解密与消息摘要"><!---->crypto - 加解密与消息摘要<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="debug.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="debug - 调试工具"><!---->debug - 调试工具<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="device.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="device - 设备"><!---->device - 设备<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="dialogs.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="dialogs - 对话框"><!---->dialogs - 对话框<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="engines.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="engines - 脚本引擎"><!---->engines - 脚本引擎<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="events.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="events - 事件与监听"><!---->events - 事件与监听<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="floaty.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="floaty - 悬浮窗"><!---->floaty - 悬浮窗<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="files.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="files - 文件系统"><!---->files - 文件系统<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="globals.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="globals - 全局变量与函数"><!---->globals - 全局变量与函数<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="http.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="http - HTTP网络请求"><!---->http - HTTP网络请求<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="media.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="media - 多媒体"><!---->media - 多媒体<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="modules.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="module - 模块"><!---->module - 模块<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="plugins.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="plugins - 插件"><!---->plugins - 插件<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="powerManager.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="power_manager - 电源管理"><!---->power_manager - 电源管理<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a aria-current="page" href="sensors.html"
                    class="router-link-active router-link-exact-active nav-link sidebar-link sidebar-page"
                    aria-label="sensors - 传感器"><!---->sensors - 传感器<!----></a>
                  <ul class="sidebar-sub-headers">
                    <li class="sidebar-sub-header"><a aria-current="page"
                        href="https://pro.autojs.org/docs/zh/v8/sensors.html#sensors-register-sensorname-delay"
                        class="router-link-active router-link-exact-active nav-link sidebar-link heading"
                        aria-label="sensors.register(sensorName[, delay])"><!---->sensors.register(sensorName[,
                        delay])<!----></a>
                      <ul class="sidebar-sub-headers"></ul>
                    </li>
                    <li class="sidebar-sub-header"><a aria-current="page"
                        href="https://pro.autojs.org/docs/zh/v8/sensors.html#sensors-unregister-emitter"
                        class="router-link-active router-link-exact-active nav-link sidebar-link heading"
                        aria-label="sensors.unregister(emitter)"><!---->sensors.unregister(emitter)<!----></a>
                      <ul class="sidebar-sub-headers"></ul>
                    </li>
                    <li class="sidebar-sub-header"><a aria-current="page"
                        href="https://pro.autojs.org/docs/zh/v8/sensors.html#sensors-unregisterall"
                        class="router-link-active router-link-exact-active nav-link sidebar-link heading"
                        aria-label="sensors.unregisterAll()"><!---->sensors.unregisterAll()<!----></a>
                      <ul class="sidebar-sub-headers"></ul>
                    </li>
                    <li class="sidebar-sub-header"><a aria-current="page"
                        href="https://pro.autojs.org/docs/zh/v8/sensors.html#sensors-ignoresunsupportedsensor"
                        class="router-link-active router-link-exact-active nav-link sidebar-link heading"
                        aria-label="sensors.ignoresUnsupportedSensor"><!---->sensors.ignoresUnsupportedSensor<!----></a>
                      <ul class="sidebar-sub-headers"></ul>
                    </li>
                    <li class="sidebar-sub-header"><a aria-current="page"
                        href="https://pro.autojs.org/docs/zh/v8/sensors.html#事件-unsupported-sensor"
                        class="router-link-active router-link-exact-active nav-link sidebar-link heading"
                        aria-label="事件: &#39;unsupported_sensor&#39;"><!---->事件: &#39;unsupported_sensor&#39;<!----></a>
                      <ul class="sidebar-sub-headers"></ul>
                    </li>
                    <li class="sidebar-sub-header"><a aria-current="page"
                        href="https://pro.autojs.org/docs/zh/v8/sensors.html#事件-change"
                        class="router-link-active router-link-exact-active nav-link sidebar-link heading"
                        aria-label="事件: &#39;change&#39;"><!---->事件: &#39;change&#39;<!----></a>
                      <ul class="sidebar-sub-headers"></ul>
                    </li>
                    <li class="sidebar-sub-header"><a aria-current="page"
                        href="https://pro.autojs.org/docs/zh/v8/sensors.html#事件-accuracy-change"
                        class="router-link-active router-link-exact-active nav-link sidebar-link heading"
                        aria-label="事件: &#39;accuracy_change&#39;"><!---->事件: &#39;accuracy_change&#39;<!----></a>
                      <ul class="sidebar-sub-headers"></ul>
                    </li>
                  </ul><!--]-->
                </li>
                <li><!--[--><a href="shell.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="shell - Shell命令"><!---->shell - Shell命令<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="storages.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="storages - 本地存储"><!---->storages - 本地存储<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="settings.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="settings - 设置"><!---->settings - 设置<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="threads.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="threads - 多线程"><!---->threads - 多线程<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="timers.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="timers - 定时器"><!---->timers - 定时器<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="timedTasks.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="work_manager - 定时与任务"><!---->work_manager - 定时与任务<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li>
                  <section class="sidebar-group"><button class="sidebar-heading clickable"><!----><span class="title">ui
                        - 用户界面</span><span class="arrow right"></span></button><!----></section>
                </li>
                <li><!--[--><a href="util.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="util - 工具"><!---->util - 工具<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="webSocket.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="WebSocket"><!---->WebSocket<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
                <li><!--[--><a href="zip.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="zip - 压缩与解压"><!---->zip - 压缩与解压<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
              </ul>
            </section>
          </li>
          <li>
            <section class="sidebar-group">
              <p class="sidebar-heading"><!----><span class="title">进阶</span><!----></p>
              <ul class="sidebar-links">
                <li><!--[--><a href="scriptingJava.html" class="nav-link sidebar-link sidebar-page"
                    aria-label="和Java交互"><!---->和Java交互<!----></a>
                  <ul class="sidebar-sub-headers"></ul><!--]-->
                </li>
              </ul>
            </section>
          </li>
        </ul><!--[--><!----><!--]-->
      </aside><!--[-->
      <main class="page" id="main-content"><!--[--><!---->
        <nav class="breadcrumb disable"></nav>
        <div class="page-title">
          <h1><!---->sensors - 传感器</h1>
          <div class="page-info"><span class="date-info" aria-label="写作日期📅" data-balloon-pos="down"><svg
                xmlns="http://www.w3.org/2000/svg" class="icon calendar-icon" viewBox="0 0 1024 1024"
                fill="currentColor" aria-label="calendar icon">
                <path
                  d="M716.4 110.137c0-18.753-14.72-33.473-33.472-33.473-18.753 0-33.473 14.72-33.473 33.473v33.473h66.993v-33.473zm-334.87 0c0-18.753-14.72-33.473-33.473-33.473s-33.52 14.72-33.52 33.473v33.473h66.993v-33.473zm468.81 33.52H716.4v100.465c0 18.753-14.72 33.473-33.472 33.473a33.145 33.145 0 01-33.473-33.473V143.657H381.53v100.465c0 18.753-14.72 33.473-33.473 33.473a33.145 33.145 0 01-33.473-33.473V143.657H180.6A134.314 134.314 0 0046.66 277.595v535.756A134.314 134.314 0 00180.6 947.289h669.74a134.36 134.36 0 00133.94-133.938V277.595a134.314 134.314 0 00-133.94-133.938zm33.473 267.877H147.126a33.145 33.145 0 01-33.473-33.473c0-18.752 14.72-33.473 33.473-33.473h736.687c18.752 0 33.472 14.72 33.472 33.473a33.145 33.145 0 01-33.472 33.473z">
                </path>
              </svg><span>2022年10月22日</span>
              <meta property="datePublished" content="2022-10-22T07:36:15.000Z">
            </span></div>
          <hr>
        </div>
        <div class="toc-place-holder">
          <aside id="toc">
            <div class="toc-header">此页内容</div>
            <div class="toc-wrapper">
              <ul class="toc-list"><!--[-->
                <li class="toc-item"><a aria-current="page"
                    href="https://pro.autojs.org/docs/zh/v8/sensors.html#sensors-register-sensorname-delay"
                    class="router-link-active router-link-exact-active toc-link level2">sensors.register(sensorName[,
                    delay])</a></li><!----><!--]--><!--[-->
                <li class="toc-item"><a aria-current="page"
                    href="https://pro.autojs.org/docs/zh/v8/sensors.html#sensors-unregister-emitter"
                    class="router-link-active router-link-exact-active toc-link level2">sensors.unregister(emitter)</a>
                </li><!----><!--]--><!--[-->
                <li class="toc-item"><a aria-current="page"
                    href="https://pro.autojs.org/docs/zh/v8/sensors.html#sensors-unregisterall"
                    class="router-link-active router-link-exact-active toc-link level2">sensors.unregisterAll()</a></li>
                <!----><!--]--><!--[-->
                <li class="toc-item"><a aria-current="page"
                    href="https://pro.autojs.org/docs/zh/v8/sensors.html#sensors-ignoresunsupportedsensor"
                    class="router-link-active router-link-exact-active toc-link level2">sensors.ignoresUnsupportedSensor</a>
                </li><!----><!--]--><!--[-->
                <li class="toc-item"><a aria-current="page"
                    href="https://pro.autojs.org/docs/zh/v8/sensors.html#事件-unsupported-sensor"
                    class="router-link-active router-link-exact-active toc-link level2">事件:
                    &#39;unsupported_sensor&#39;</a></li><!----><!--]--><!--[-->
                <li class="toc-item"><a aria-current="page"
                    href="https://pro.autojs.org/docs/zh/v8/sensors.html#事件-change"
                    class="router-link-active router-link-exact-active toc-link level2">事件: &#39;change&#39;</a></li>
                <!----><!--]--><!--[-->
                <li class="toc-item"><a aria-current="page"
                    href="https://pro.autojs.org/docs/zh/v8/sensors.html#事件-accuracy-change"
                    class="router-link-active router-link-exact-active toc-link level2">事件:
                    &#39;accuracy_change&#39;</a></li><!----><!--]-->
              </ul>
            </div>
          </aside>
        </div><!---->
        <div class="theme-hope-content">
          <h1 id="sensors-传感器" tabindex="-1"><a class="header-anchor" href="#sensors-传感器" aria-hidden="true">#</a>
            sensors - 传感器</h1>
          <blockquote>
            <p>Stability: 2 - Stable</p>
          </blockquote>
          <p>
            sensors模块提供了获取手机上的传感器的信息的支持，这些传感器包括距离传感器、光线光感器、重力传感器、方向传感器等。需要指出的是，脚本只能获取传感器的数据，<strong>不能模拟或伪造传感器的数据和事件</strong>，因此诸如模拟摇一摇的功能是无法实现的。
          </p>
          <p>
            要监听一个传感器时，需要使用<code>sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>注册监听器，之后才能开始监听；不需要监听时则调用<code>sensors<span class="token punctuation">.</span><span class="token function">unregister</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>注销监听器。在脚本结束时会自动注销所有的监听器。同时，这种监听会使脚本保持运行状态，如果不注销监听器，脚本会一直保持运行状态。
          </p>
          <p>例如，监听光线传感器的代码为：</p>
          <div class="language-javascript ext-js line-numbers-mode">
            <pre class="language-javascript"><code><span class="token comment">//光线传感器监听</span>
sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">&quot;light&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&quot;change&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">event<span class="token punctuation">,</span> light</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;当前光强度为&quot;</span><span class="token punctuation">,</span> light<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
            <div class="line-numbers" aria-hidden="true">
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
            </div>
          </div>
          <p>
            要注意的是，每个传感器的数据并不相同，所以对他们调用<code><span class="token function">on</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>监听事件时的回调函数参数也不是相同，例如光线传感器参数为<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> light<span class="token punctuation">)</span></code>，加速度传感器参数为<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> ax<span class="token punctuation">,</span> ay<span class="token punctuation">,</span> az<span class="token punctuation">)</span></code>。甚至在某些设备上的传感器参数有所增加，例如华为手机的距离传感器为三个参数，一般手机只有一个参数。
          </p>
          <p>常用的传感器及其事件参数如下表：</p>
          <ul>
            <li>
              <p><code><span class="token property">accelerometer</span></code>
                加速度传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> ax<span class="token punctuation">,</span> ay<span class="token punctuation">,</span> az<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">ax</span></code> {number} x轴上的加速度，单位m/s^2</li>
                <li><code><span class="token property">ay</span></code> {number} y轴上的加速度，单位m/s^2</li>
                <li><code><span class="token property">az</span></code> {number} z轴上的加速度，单位m/s^2
                  这里的x轴，y轴，z轴所属的坐标系统如下图(其中z轴垂直于设备屏幕表面):</li>
              </ul>
              <p>!<img src="#images/axis_device.png" alt="axis_device" loading="lazy"></p>
            </li>
            <li>
              <p><code><span class="token property">orientation</span></code>
                方向传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> azimuth<span class="token punctuation">,</span> pitch<span class="token punctuation">,</span> roll<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">azimuth</span></code> {number}
                  方位角，从地磁指北方向线起，依顺时针方向到y轴之间的水平夹角，单位角度，范围0~359</li>
                <li><code><span class="token property">pitch</span></code> {number}
                  绕x轴旋转的角度，当设备水平放置时该值为0，当设备顶部翘起时该值为正数，当设备尾部翘起时该值为负数，单位角度，范围-180~180</li>
                <li><code><span class="token property">roll</span></code> {number} 绕y轴顺时针旋转的角度，单位角度，范围-90~90</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">gyroscope</span></code>
                陀螺仪传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> wx<span class="token punctuation">,</span> wy<span class="token punctuation">,</span> wz<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">wx</span></code> {number} 绕x轴的角速度，单位弧度/s</li>
                <li><code><span class="token property">wy</span></code> {number} 绕y轴的角速度，单位弧度/s</li>
                <li><code><span class="token property">wz</span></code> {number} 绕z轴的角速度，单位弧度/s</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">magnetic_field</span></code>
                磁场传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> bx<span class="token punctuation">,</span> by<span class="token punctuation">,</span> bz<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">bx</span></code> {number} x轴上的磁场强度，单位uT</li>
                <li><code><span class="token property">by</span></code> {number} y轴上的磁场强度，单位uT</li>
                <li><code><span class="token property">bz</span></code> {number} z轴上的磁场强度，单位uT</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">gravity</span></code>
                重力传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> gx<span class="token punctuation">,</span> gy<span class="token punctuation">,</span> gz<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">gx</span></code> {number} x轴上的重力加速度，单位m/s^2</li>
                <li><code><span class="token property">gy</span></code> {number} y轴上的重力加速度，单位m/s^2</li>
                <li><code><span class="token property">gz</span></code> {number} z轴上的重力加速度，单位m/s^2</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">linear_acceleration</span></code>
                线性加速度传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> ax<span class="token punctuation">,</span> ay<span class="token punctuation">,</span> az<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">ax</span></code> {number} x轴上的线性加速度，单位m/s^2</li>
                <li><code><span class="token property">ay</span></code> {number} y轴上的线性加速度，单位m/s^2</li>
                <li><code><span class="token property">az</span></code> {number} z轴上的线性加速度，单位m/s^2</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">ambient_temperature</span></code>
                环境温度传感器，大部分设备并不支持，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> t<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">t</span></code> {number} 环境温度，单位摄氏度。</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">light</span></code>
                光线传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> light<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">light</span></code> {number} 环境光强度，单位lux</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">pressure</span></code>
                压力传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> p<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">p</span></code> {number} 大气压，单位hPa</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">proximity</span></code>
                距离传感器，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> distance<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">distance</span></code> {number}
                  一般指设备前置摄像头旁边的距离传感器到前方障碍物的距离，并且很多设备上这个值只有两种情况：当障碍物较近时该值为0，当障碍物较远或在范围内没有障碍物时该值为5</li>
              </ul>
            </li>
            <li>
              <p><code><span class="token property">relative_humidity</span></code>
                湿度传感器，大部分设备并不支持，参数<code><span class="token punctuation">(</span>event<span class="token punctuation">,</span> rh<span class="token punctuation">)</span></code>:
              </p>
              <ul>
                <li><code><span class="token property">event</span></code> <a
                    href="#sensors_sensorevent">SensorEvent</a> 传感器事件，用于获取传感器数据变化时的所有信息</li>
                <li><code><span class="token property">rh</span></code> {number} 相对湿度，范围为0~100（百分比）</li>
              </ul>
            </li>
          </ul>
          <h2 id="sensors-register-sensorname-delay" tabindex="-1"><a class="header-anchor"
              href="#sensors-register-sensorname-delay" aria-hidden="true">#</a> sensors.register(sensorName[, delay])
          </h2>
          <ul>
            <li><code><span class="token property">sensorName</span></code> {string} 传感器名称，常用的传感器名称如上面所述</li>
            <li><code><span class="token property">delay</span></code> {number}
              传感器数据更新频率，可选，默认为<code>sensors<span class="token punctuation">.</span>delay<span class="token punctuation">.</span>normal</code>。可用的值如下：
              <ul>
                <li>
                  <code>sensors<span class="token punctuation">.</span>delay<span class="token punctuation">.</span>normal</code>
                  正常频率</li>
                <li>
                  <code>sensors<span class="token punctuation">.</span>delay<span class="token punctuation">.</span>ui</code>
                  适合于用户界面的更新频率</li>
                <li>
                  <code>sensors<span class="token punctuation">.</span>delay<span class="token punctuation">.</span>game</code>
                  适合于游戏的更新频率</li>
                <li>
                  <code>sensors<span class="token punctuation">.</span>delay<span class="token punctuation">.</span>fastest</code>
                  最快的更新频率】</li>
              </ul>
            </li>
            <li>返回 <a href="#sensors_sensoreventemitter">SensorEventEmitter</a></li>
          </ul>
          <p>注册一个传感器监听并返回<a href="#sensors_sensoreventemitter">SensorEventEmitter</a>。</p>
          <p>例如:</p>
          <div class="language-javascript ext-js line-numbers-mode">
            <pre class="language-javascript"><code>console<span class="token punctuation">.</span><span class="token function">show</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//注册传感器监听</span>
<span class="token keyword">var</span> sensor <span class="token operator">=</span> sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">&quot;gravity&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>sensor <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">toast</span><span class="token punctuation">(</span><span class="token string">&quot;不支持重力传感器&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//监听数据</span>
sensor<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&quot;change&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">gx<span class="token punctuation">,</span> gy<span class="token punctuation">,</span> gz</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;重力加速度: %d, %d, %d&quot;</span><span class="token punctuation">,</span> gx<span class="token punctuation">,</span> gy<span class="token punctuation">,</span> gz<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
            <div class="line-numbers" aria-hidden="true">
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
            </div>
          </div>
          <p>可以通过delay参数来指定传感器数据的更新频率，例如：</p>
          <div class="language-javascript ext-js line-numbers-mode">
            <pre class="language-javascript"><code><span class="token keyword">var</span> sensor <span class="token operator">=</span> sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">&quot;gravity&quot;</span><span class="token punctuation">,</span> sensors<span class="token punctuation">.</span>delay<span class="token punctuation">.</span>game<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
            <div class="line-numbers" aria-hidden="true">
              <div class="line-number"></div>
            </div>
          </div>
          <p>
            另外，如果不支持<code><span class="token property">sensorName</span></code>所指定的传感器，那么该函数将返回<code><span class="token keyword">null</span></code>；但如果<code>sensors<span class="token punctuation">.</span>ignoresUnsupportedSensor</code>的值被设置为<code><span class="token boolean">true</span></code>,
            则该函数会返回一个不会分发任何传感器事件的<a href="#sensors_sensoreventemitter">SensorEventEmitter</a>。</p>
          <p>例如:</p>
          <div class="language-javascript ext-js line-numbers-mode">
            <pre class="language-javascript"><code>sensors<span class="token punctuation">.</span>ignoresUnsupportedSensor <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token comment">//无需null判断</span>
sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">&quot;gravity&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&quot;change&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">gx<span class="token punctuation">,</span> gy<span class="token punctuation">,</span> gz</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    <span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;重力加速度: %d, %d, %d&quot;</span><span class="token punctuation">,</span> gx<span class="token punctuation">,</span> gy<span class="token punctuation">,</span> gz<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
            <div class="line-numbers" aria-hidden="true">
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
            </div>
          </div>
          <p>更多信息，参见<a href="#sensors_sensoreventemitter">SensorEventEmitter</a>和<a
              href="#sensors_sensors_ignoresUnsupportedSensor">sensors.ignoresUnsupportedSensor</a>。</p>
          <h2 id="sensors-unregister-emitter" tabindex="-1"><a class="header-anchor" href="#sensors-unregister-emitter"
              aria-hidden="true">#</a> sensors.unregister(emitter)</h2>
          <ul>
            <li><code><span class="token property">emitter</span></code> {<a
                href="#sensors_sensoreventemitter">SensorEventEmitter</a>}</li>
          </ul>
          <p>注销该传感器监听器。被注销的监听器将不再能监听传感器数据。</p>
          <div class="language-javascript ext-js line-numbers-mode">
            <pre class="language-javascript"><code><span class="token comment">//注册一个传感器监听器</span>
<span class="token keyword">var</span> sensor <span class="token operator">=</span> sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">&quot;gravity&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>sensor <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">exit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//2秒后注销该监听器</span>
<span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
    sensors<span class="token punctuation">.</span><span class="token function">unregister</span><span class="token punctuation">(</span>sensor<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">2000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
            <div class="line-numbers" aria-hidden="true">
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
            </div>
          </div>
          <h2 id="sensors-unregisterall" tabindex="-1"><a class="header-anchor" href="#sensors-unregisterall"
              aria-hidden="true">#</a> sensors.unregisterAll()</h2>
          <p>注销所有传感器监听器。</p>
          <h2 id="sensors-ignoresunsupportedsensor" tabindex="-1"><a class="header-anchor"
              href="#sensors-ignoresunsupportedsensor" aria-hidden="true">#</a> sensors.ignoresUnsupportedSensor</h2>
          <ul>
            <li>{boolean}</li>
          </ul>
          <p>
            表示是否忽略不支持的传感器。如果该值被设置为<code><span class="token boolean">true</span></code>，则函数<code>sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>即使对不支持的传感器也会返回一个无任何数据的虚拟传感器监听，也就是<code>sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>不会返回<code><span class="token keyword">null</span></code>从而避免非空判断，并且此时会触发<code><span class="token property">sensors</span></code>的&quot;unsupported_sensor&quot;事件。
          </p>
          <div class="language-javascript ext-js line-numbers-mode">
            <pre class="language-javascript"><code><span class="token comment">//忽略不支持的传感器</span>
sensors<span class="token punctuation">.</span>ignoresUnsupportedSensor <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token comment">//监听有不支持的传感器时的事件</span>
sensors<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&quot;unsupported_sensor&quot;</span><span class="token punctuation">,</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">sensorName</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">toastLog</span><span class="token punctuation">(</span><span class="token string">&quot;不支持的传感器: &quot;</span> <span class="token operator">+</span> sensorName<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//随便注册一个不存在的传感器。</span>
<span class="token function">log</span><span class="token punctuation">(</span>sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">&quot;aaabbb&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
            <div class="line-numbers" aria-hidden="true">
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
            </div>
          </div>
          <h2 id="事件-unsupported-sensor" tabindex="-1"><a class="header-anchor" href="#事件-unsupported-sensor"
              aria-hidden="true">#</a> 事件: &#39;unsupported_sensor&#39;</h2>
          <ul>
            <li><code><span class="token property">sensorName</span></code> {string} 不支持的传感器名称</li>
          </ul>
          <p>
            当<code>sensors<span class="token punctuation">.</span>ignoresUnsupportedSensor</code>被设置为<code><span class="token boolean">true</span></code>并且有不支持的传感器被注册时触发该事件。事件参数的传感器名称。
          </p>
          <h1 id="sensoreventemitter" tabindex="-1"><a class="header-anchor" href="#sensoreventemitter"
              aria-hidden="true">#</a> SensorEventEmitter</h1>
          <p>注册传感器返回的对象，其本身是一个EventEmitter，用于监听传感器事件。</p>
          <h2 id="事件-change" tabindex="-1"><a class="header-anchor" href="#事件-change" aria-hidden="true">#</a> 事件:
            &#39;change&#39;</h2>
          <ul>
            <li><code><span class="token punctuation">.</span><span class="token punctuation">.</span>args</code> {Any}
              传感器参数</li>
          </ul>
          <p>
            当传感器数据改变时触发该事件；该事件触发的最高频繁由<code>sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token punctuation">)</span></code>指定的delay参数决定。
          </p>
          <p>事件参数根据传感器类型不同而不同，具体参见本章最前面的列表。</p>
          <p>一个监听光线传感器和加速度传感器并且每0.5秒获取一个数据并最终写入一个csv表格文件的例子如下：</p>
          <div class="language-javascript ext-js line-numbers-mode">
            <pre class="language-javascript"><code><span class="token comment">//csv文件路径</span>
<span class="token keyword">const</span> csvPath <span class="token operator">=</span> <span class="token string">&quot;/sdcard/sensors_data.csv&quot;</span><span class="token punctuation">;</span>
<span class="token comment">//记录光线传感器的数据</span>
<span class="token keyword">var</span> light <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token comment">//记录加速度传感器的数据</span>
<span class="token keyword">var</span> ax <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> ay <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> az <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token comment">//监听光线传感器</span>
sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">&quot;light&quot;</span><span class="token punctuation">,</span> sensors<span class="token punctuation">.</span>delay<span class="token punctuation">.</span>fastest<span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&quot;change&quot;</span><span class="token punctuation">,</span> <span class="token parameter">l</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        light <span class="token operator">=</span> l<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//监听加速度传感器</span>
sensors<span class="token punctuation">.</span><span class="token function">register</span><span class="token punctuation">(</span><span class="token string">&quot;accelerometer&quot;</span><span class="token punctuation">,</span> sensors<span class="token punctuation">.</span>delay<span class="token punctuation">.</span>fastest<span class="token punctuation">)</span>
    <span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">&quot;change&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">ax0<span class="token punctuation">,</span> ay0<span class="token punctuation">,</span> az0</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
        ax <span class="token operator">=</span> ax0<span class="token punctuation">;</span>
        ay <span class="token operator">=</span> ay0<span class="token punctuation">;</span>
        az <span class="token operator">=</span> az0<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> file <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span>csvPath<span class="token punctuation">,</span> <span class="token string">&quot;w&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//写csv表格头</span>
file<span class="token punctuation">.</span><span class="token function">writeline</span><span class="token punctuation">(</span><span class="token string">&quot;light,ax,ay,az&quot;</span><span class="token punctuation">)</span>
<span class="token comment">//每0.5秒获取一次数据并写入文件</span>
<span class="token function">setInterval</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    file<span class="token punctuation">.</span><span class="token function">writeline</span><span class="token punctuation">(</span>util<span class="token punctuation">.</span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string">&quot;%d,%d,%d,%d&quot;</span><span class="token punctuation">,</span> light<span class="token punctuation">,</span> ax<span class="token punctuation">,</span> ay<span class="token punctuation">,</span> az<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//10秒后退出并打开文件</span>
<span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">=&gt;</span><span class="token punctuation">{</span>
    file<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    sensors<span class="token punctuation">.</span><span class="token function">unregisterAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    app<span class="token punctuation">.</span><span class="token function">viewFile</span><span class="token punctuation">(</span>csvPath<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">10</span> <span class="token operator">*</span> <span class="token number">1000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre>
            <div class="line-numbers" aria-hidden="true">
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
              <div class="line-number"></div>
            </div>
          </div>
          <h2 id="事件-accuracy-change" tabindex="-1"><a class="header-anchor" href="#事件-accuracy-change"
              aria-hidden="true">#</a> 事件: &#39;accuracy_change&#39;</h2>
          <ul>
            <li><code><span class="token property">accuracy</span></code> {number} 表示传感器精度。为以下值之一: <ul>
                <li>-1 传感器未连接</li>
                <li>0 传感器不可读</li>
                <li>1 低精度</li>
                <li>2 中精度</li>
                <li>3 高精度</li>
              </ul>
            </li>
          </ul>
          <p>当传感器精度改变时会触发的事件。比较少用。</p>
        </div><!---->
        <footer class="page-meta"><!---->
          <div class="meta-item update-time"><span class="label">上次编辑于: </span><!----></div>
          <div class="meta-item contributors"><span class="label">贡献者: </span><!--[--><!--[--><span class="contributor"
              title="email: 946994919@qq.com">hyb1996</span><!--]--><!--]--></div>
        </footer>
        <nav class="page-nav"><a href="powerManager.html" class="nav-link prev" aria-label="power_manager - 电源管理">
            <div class="hint"><span class="arrow left"></span>上一页</div>
            <div class="link"><!---->power_manager - 电源管理</div>
          </a><a href="shell.html" class="nav-link next" aria-label="shell - Shell命令">
            <div class="hint">下一页<span class="arrow right"></span></div>
            <div class="link">shell - Shell命令<!----></div>
          </a></nav><!----><!----><!--]-->
      </main><!--]--><!----><!--]-->
    </div><!--]--><!----><!----><!--]-->
  </div>
  <script type="module" src="../../assets/app.6aa2b576.js" defer></script>
</body>

</html>